package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.SearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zimmor
 * @Date: 2021/3/6 17:07
 * @Description
 * @version: 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/article")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article) {
        searchService.add(article);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    @RequestMapping(value = "/search/{keyword}/{page}/{size}")
    public Result findByKeyword(@PathVariable String keyword, @PathVariable int page, @PathVariable int size) {
        Page pageData = searchService.findByKeyWord(keyword, page, size);
        return new Result(true, StatusCode.OK, "查找成功", new PageResult<Article>(pageData.getTotalElements(),pageData.getContent()));
    }
}
